<?php
if (! function_exists ( "pageft" )) {
  /**
   * 对得到的数据分页
   *
   * @param 数据总数 $totle
   * @param 每页显示数量 $displaypg
   * @param 分页连接地址 $url
   * @param 附加信息 $add
   * @return 分页信息
   */
  function pageft($pageNumber,$totle, $year, $geoId,$firstCateId,$lastCateId,$displaypg = 20, $url = '', $add = false, $keywords = "") {
    global $firstcount, $pagenav, $_SERVER, $page_header;
    $GLOBALS ["displaypg"] = $displaypg;
    if (isset ( $_GET ['page'] )) {
      if (! intval ( $_GET ['page'] ))
        $page = 1;
      else
        $page = $_GET ['page'];
    } else {
      $page = 1;
    }
    $page = $pageNumber;
    if (! $url) {
      $url = $_SERVER ["REQUEST_URI"];
    }
    // URL分析：
    $parse_url = parse_url ( $url );
    $url_query = $parse_url ["query"]; //单独取出URL的查询字串
    if ($url_query) {
      // 因为URL中可能包含了页码信息，我们要把它去掉，以便加入新的页码信息。
      // 这里用到了正则表达式，请参考“PHP中的正规表达式”.
      $url_query = ereg_replace ( "(^|&)page=$page", "", $url_query );
      // 将处理后的URL的查询字串替换原来的URL的查询字串：
      $url = str_replace ( $parse_url ["query"], $url_query, $url );
      // 在URL后加page查询信息，但待赋值：
      if ($url_query)
        $url .= "&page";
      else
        $url .= "page";
    } else {
      $url .= "?page";
    }
    // 页码计算：
    $lastpg = ceil ( $totle / $displaypg ); //最后页，也是总页数
    $page = min ( $lastpg, $page );
    $prepg = $page - 1; //上一页
    $nextpg = ($page == $lastpg ? 0 : $page + 1); //下一页
    $firstcount = ($page - 1) * $displaypg;
    if ($firstcount < 0) {
      $firstcount = 0;
    }
    $page_header = "共<strong><span style=\"COLOR: #ff6600\">" . $lastpg . "</span></strong>页&nbsp;&nbsp;每页显示<strong><span style=\"COLOR: #ff6600\">" . $displaypg . "</span></strong>条记录,目前显示的是第<strong><span style=\"COLOR: #ff6600\">" . $page . "</span></strong>页.";
    //$pagenav = "<br />";
    $pagenav = "";
    $keywords = "\"".$keywords."\"";
    //echo "---".$keywords;
    if ($lastpg <= 1)
      return false;
    $pagenav .= "页次：<strong>" . $page . "</strong>/<strong>" . $lastpg . "</strong>&nbsp;&nbsp;";
    if ($prepg)
      //$pagenav .= " <a href='$url=1'>首页</a>&nbsp;<a href='$url=$prepg'>上一页</a> ";
      $pagenav .= "<a onClick='javascript:cunwuq($year);xajax_sendContentRequest($year,1,$geoId,$firstCateId,$keywords);' href='javascript:void(0)' >首页</a>&nbsp;<a
        onClick='javascript:cunwuq($year);xajax_sendContentRequest($year,$prepg,$geoId,$firstCateId,$keywords);' href='javascript:void(0)' >上一页</a> ";
    else
      $pagenav .= " ";
    $prevs = $page - 5;
    if ($prevs <= 0) {
      $prevs = 1;
    }
    $prev = $prevs - 1;
    if ($prev <= 0) {
      $prev = 1;
    }
    $nexts = $page + 4;
    if ($nexts > $lastpg) {
      $nexts = $lastpg;
    }
    $next = $nexts + 1;
    if ($next > $lastpg) {
      $next = $lastpg;
    }
    for($i = $prevs; $i <= $page - 1; $i ++) {
      $pagenav .= "&nbsp;[<a onClick='javascript:cunwuq($year);xajax_sendContentRequest($year,$i,$geoId,$firstCateId,$keywords);' href='javascript:void(0)'>$i</a>]";
    }
    $pagenav .= "&nbsp;<strong><span class=\"fontred\">$page</span></strong>";
    $title = null;

    for($i = $page + 1; $i <= $nexts; $i ++) {
      if ($i == $lastpg)
        $title = "title=\"这是最后一页\"";
      $pagenav .= "&nbsp;[<a onClick='javascript:cunwuq($year);xajax_sendContentRequest($year,$i,$geoId,$firstCateId,$keywords);' href='javascript:void(0)' $title>$i</a>]";
    }

    if ($nextpg)
      $pagenav .= "<a onClick='javascript:cunwuq($year);xajax_sendContentRequest($year,$nextpg,$geoId,$firstCateId,$keywords);' href='javascript:void(0)' >下一页</a>&nbsp;<a
        onClick='javascript:cunwuq($year);xajax_sendContentRequest($year,$lastpg,$geoId,$firstCateId,$keywords);' href='javascript:void(0)' >尾页</a> ";
    else
      $pagenav .= " ";

    return $pagenav;
  }

  function pageftHomepage($pageNumber,$totle, $geoId,$categoryCode,$categoryName,$displaypg = 20, $url = '', $add = false) {
    $categoryName = '"' . $categoryName . '"' ;
    global $firstcount, $pagenav, $_SERVER, $page_header;
    $GLOBALS ["displaypg"] = $displaypg;
    if (isset ( $_GET ['page'] )) {
      if (! intval ( $_GET ['page'] ))
        $page = 1;
      else
        $page = $_GET ['page'];
    } else {
      $page = 1;
    }
    $page = $pageNumber;
    if (! $url) {
      $url = $_SERVER ["REQUEST_URI"];
    }
    // URL分析：
    $parse_url = parse_url ( $url );
    $url_query = $parse_url ["query"]; //单独取出URL的查询字串
    if ($url_query) {
      // 因为URL中可能包含了页码信息，我们要把它去掉，以便加入新的页码信息。
      // 这里用到了正则表达式，请参考“PHP中的正规表达式”.
      $url_query = ereg_replace ( "(^|&)page=$page", "", $url_query );
      // 将处理后的URL的查询字串替换原来的URL的查询字串：
      $url = str_replace ( $parse_url ["query"], $url_query, $url );
      // 在URL后加page查询信息，但待赋值：
      if ($url_query)
        $url .= "&page";
      else
        $url .= "page";
    } else {
      $url .= "?page";
    }
    // 页码计算：
    $lastpg = ceil ( $totle / $displaypg ); //最后页，也是总页数
    $page = min ( $lastpg, $page );
    $prepg = $page - 1; //上一页
    $nextpg = ($page == $lastpg ? 0 : $page + 1); //下一页
    $firstcount = ($page - 1) * $displaypg;
    if ($firstcount < 0) {
      $firstcount = 0;
    }
    $page_header = "共<strong><span style=\"COLOR: #ff6600\">" . $lastpg . "</span></strong>页&nbsp;&nbsp;每页显示<strong><span style=\"COLOR: #ff6600\">" . $displaypg . "</span></strong>条记录,目前显示的是第<strong><span style=\"COLOR: #ff6600\">" . $page . "</span></strong>页.";
    //$pagenav = "<br />";
    $pagenav = "";
    if ($lastpg <= 1)
      return false;
    $pagenav .= "页次：<strong>" . $page . "</strong>/<strong>" . $lastpg . "</strong>&nbsp;&nbsp;";
    if ($prepg)
      //$pagenav .= " <a href='$url=1'>首页</a>&nbsp;<a href='$url=$prepg'>上一页</a> ";
      $pagenav .= "<a onClick='xajax_getElement($geoId,$categoryCode,$categoryName,1);' href='javascript:void(0)' >首页</a>&nbsp;<a
        onClick='javascript:xajax_getElement($geoId,$categoryCode,$categoryName,$prepg);' href='javascript:void(0)' >上一页</a> ";
    else
      $pagenav .= " ";
    $prevs = $page - 5;
    if ($prevs <= 0) {
      $prevs = 1;
    }
    $prev = $prevs - 1;
    if ($prev <= 0) {
      $prev = 1;
    }
    $nexts = $page + 4;
    if ($nexts > $lastpg) {
      $nexts = $lastpg;
    }
    $next = $nexts + 1;
    if ($next > $lastpg) {
      $next = $lastpg;
    }
    for($i = $prevs; $i <= $page - 1; $i ++) {
      $pagenav .= "&nbsp;[<a onClick='javascript:xajax_getElement($geoId,$categoryCode,$categoryName,$i);' href='javascript:void(0)'>$i</a>]";
    }
    $pagenav .= "&nbsp;<strong><span class=\"fontred\">$page</span></strong>";
    $title = null;
    for($i = $page + 1; $i <= $nexts; $i ++) {
      if ($i == $lastpg)
        $title = "title=\"这是最后一页\"";
      $pagenav .= "&nbsp;[<a onClick='javascript:xajax_getElement($geoId,$categoryCode,$categoryName,$i);' href='javascript:void(0)' $title>$i</a>]";
    }

    if ($nextpg)
      $pagenav .= "<a onClick='javascript:xajax_getElement($geoId,$categoryCode,$categoryName,$nextpg);' href='javascript:void(0)' >下一页</a>&nbsp;<a
        onClick='javascript:xajax_getElement($geoId,$categoryCode,$categoryName,$lastpg);' href='javascript:void(0)' >尾页</a> ";
    else
      $pagenav .= " ";

    return $pagenav;
  }
  function pageftProduct($pageNumber,$totle, $categoryCode, $keyword, $type=-1 ,$displaypg = 20, $url = '', $add = false) {
    $keyword = '"' . $keyword . '"' ;
    global $firstcount, $pagenav, $_SERVER, $page_header;
    $GLOBALS ["displaypg"] = $displaypg;
    if (isset ( $_GET ['page'] )) {
      if (! intval ( $_GET ['page'] ))
        $page = 1;
      else
        $page = $_GET ['page'];
    } else {
      $page = 1;
    }
    $page = $pageNumber;
    if (! $url) {
      $url = $_SERVER ["REQUEST_URI"];
    }
    // URL分析：
    $parse_url = parse_url ( $url );
    $url_query = $parse_url ["query"]; //单独取出URL的查询字串
    if ($url_query) {
      // 因为URL中可能包含了页码信息，我们要把它去掉，以便加入新的页码信息。
      // 这里用到了正则表达式，请参考“PHP中的正规表达式”.
      $url_query = ereg_replace ( "(^|&)page=$page", "", $url_query );
      // 将处理后的URL的查询字串替换原来的URL的查询字串：
      $url = str_replace ( $parse_url ["query"], $url_query, $url );
      // 在URL后加page查询信息，但待赋值：
      if ($url_query)
        $url .= "&page";
      else
        $url .= "page";
    } else {
      $url .= "?page";
    }
    // 页码计算：
    $lastpg = ceil ( $totle / $displaypg ); //最后页，也是总页数
    $page = min ( $lastpg, $page );
    $prepg = $page - 1; //上一页
    $nextpg = ($page == $lastpg ? 0 : $page + 1); //下一页
    $firstcount = ($page - 1) * $displaypg;
    if ($firstcount < 0) {
      $firstcount = 0;
    }
    $page_header = "共<strong><span style=\"COLOR: #ff6600\">" . $lastpg . "</span></strong>页&nbsp;&nbsp;每页显示<strong><span style=\"COLOR: #ff6600\">" . $displaypg . "</span></strong>条记录,目前显示的是第<strong><span style=\"COLOR: #ff6600\">" . $page . "</span></strong>页.";
    //$pagenav = "<br />";
    $pagenav = "";
    if ($lastpg <= 1)
      return false;
  //  $pagenav .= "页次：<strong>" . $page . "</strong>/<strong>" . $lastpg . "</strong>&nbsp;&nbsp;";
    $pagenav .= "<strong>" . $page . "</strong>/<strong>" . $lastpg . "</strong>&nbsp;&nbsp;";
    if ($prepg)
      $pagenav .= "<a onClick='xajax_getProduct($categoryCode, $keyword,$type,1);' href='javascript:void(0)'>
      <img height='11' border='0' width='22' alt='' src='images/cwgk_pro_page_prv2.gif'/>
      </a>&nbsp;<a onClick='javascript:xajax_getProduct($categoryCode , $keyword,$type, $prepg);' href='javascript:void(0)' >
      <img height='11' border='0' width='22' alt='' src='images/cwgk_pro_page_prv.gif'/>
      </a> ";
    else
      $pagenav .= " ";
    $prevs = $page - 2; //step size
    if ($prevs <= 0) {
      $prevs = 1;
    }
    $prev = $prevs - 1;
    if ($prev <= 0) {
      $prev = 1;
    }
    $nexts = $page + 2; //step size
    if ($nexts > $lastpg) {
      $nexts = $lastpg;
    }
    $next = $nexts + 1;
    if ($next > $lastpg) {
      $next = $lastpg;
    }
    for($i = $prevs; $i <= $page - 1; $i ++) {
      $pagenav .= "&nbsp;[<a onClick='javascript:xajax_getProduct($categoryCode, $keyword, $type ,$i);' href='javascript:void(0)'>$i</a>]";
    }
    $pagenav .= "&nbsp;<strong><span class=\"fontred\">$page</span></strong>";
    $title = null;
    for($i = $page + 1; $i <= $nexts; $i ++) {
      if ($i == $lastpg)
        $title = "title=\"这是最后一页\"";
      $pagenav .= "&nbsp;[<a onClick='javascript:xajax_getProduct($categoryCode, $keyword,$type , $i);' href='javascript:void(0)' $title>$i</a>]";
    }

    if ($nextpg)
      $pagenav .= "<a onClick='javascript:xajax_getProduct($categoryCode, $keyword,$type , $nextpg);' href='javascript:void(0)' >
      <img height='11' border='0' width='22' alt='' src='images/cwgk_pro_page_next.gif'/>
      </a>&nbsp;<a
        onClick='javascript:xajax_getProduct($categoryCode, $keyword, $type ,$lastpg);' href='javascript:void(0)' >
        <img height='11' border='0' width='22' alt='' src='images/cwgk_pro_page_next2.gif'/> 
        </a> ";
    else
      $pagenav .= " ";

    return $pagenav;
  }

}
?>
